package com.atguigu.lease.web.admin.controller.apartment;

import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.AttrKey;
import com.atguigu.lease.model.entity.AttrValue;
import com.atguigu.lease.web.admin.service.AttrKeyService;
import com.atguigu.lease.web.admin.service.AttrValueService;
import com.atguigu.lease.web.admin.vo.attr.AttrKeyVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: AttrController
 * Package: com.atguigu.lease.web.admin.controller.apartment
 * Description:
 *
 * @Author ygf
 * @Create 2024/9/13 10:15
 * @Version 1.0
 */
@Tag(name = "房间基本信息管理接口")
@RestController
@RequestMapping("/admin/attr")
public class AttrController {

    @Autowired
    private AttrKeyService attrkeyService;
    @Autowired
    AttrValueService attrValueService;

    @Operation(summary = "保存或更新属性名")
    @PostMapping("/key/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody AttrKey attrKey){
        //调用service层
        attrkeyService.saveOrUpdate(attrKey);
        return Result.ok();
    }

    @Operation(summary = "保存或更新属性值")
    @PostMapping("value/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody AttrValue attrValue){
        attrValueService.saveOrUpdate(attrValue);
        return Result.ok();
    }

    //TODO 查询数据不对 已解决sql有问题
    @Operation(summary = "查询全部属性名称和属性值列表")
    @GetMapping("list")
    public Result<List<AttrKeyVo>> listAttrInfo(){
        //TODO 持久化层的resultMap的id怎么起名字？
        //调用service层
        List<AttrKeyVo> attrKeyVoList = attrkeyService.listAttrInfo();
        return Result.ok(attrKeyVoList);
    }

    @Operation(summary = "根据ID删除属性名称")
    @DeleteMapping("/key/deleteById")
    public Result deleteAttrKeyById(@RequestParam Long attrKeyId){
        //
        // TODO //删除key——是否意味着也删除了value
        attrkeyService.removeById(attrKeyId);
        LambdaQueryWrapper<AttrValue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AttrValue::getAttrKeyId,attrKeyId);
        attrValueService.remove(queryWrapper);
        return Result.ok();
    }

    @Operation(summary = "根据ID删除属性值")
    @DeleteMapping("/value/deleteById")
    public Result deleteAttrValueById(@RequestParam Long id){
        attrValueService.removeById(id);
        return Result.ok();
    }



}
